{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 第 9 节　均值差的检验\n",
    "## 第 3 章　使用 Python 进行数据分析｜用 Python 动手学统计学"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 3. 实现：实验准备"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [],
   "source": [
    "# 用于数值计算的库\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "# 用于绘图的库\n",
    "import seaborn as sns\n",
    "from scipy import stats\n",
    "\n",
    "sns.set()\n",
    "\n",
    "# 设置浮点数打印精度\n",
    "%precision 3\n",
    "# 在 Jupyter Notebook 里显示图形\n",
    "%matplotlib inline"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T20:13:07.953290Z",
     "end_time": "2024-04-16T20:13:07.982773Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "data": {
      "text/plain": "  person medicine  body_temperature\n0      A   before              36.2\n1      B   before              36.2\n2      C   before              35.3\n3      D   before              36.1\n4      E   before              36.1\n5      A    after              36.8\n6      B    after              36.1\n7      C    after              36.8\n8      D    after              37.1\n9      E    after              36.9",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>person</th>\n      <th>medicine</th>\n      <th>body_temperature</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>A</td>\n      <td>before</td>\n      <td>36.2</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>B</td>\n      <td>before</td>\n      <td>36.2</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>C</td>\n      <td>before</td>\n      <td>35.3</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>D</td>\n      <td>before</td>\n      <td>36.1</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>E</td>\n      <td>before</td>\n      <td>36.1</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>A</td>\n      <td>after</td>\n      <td>36.8</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>B</td>\n      <td>after</td>\n      <td>36.1</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>C</td>\n      <td>after</td>\n      <td>36.8</td>\n    </tr>\n    <tr>\n      <th>8</th>\n      <td>D</td>\n      <td>after</td>\n      <td>37.1</td>\n    </tr>\n    <tr>\n      <th>9</th>\n      <td>E</td>\n      <td>after</td>\n      <td>36.9</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 读入数据\n",
    "paired_test_data = pd.read_csv(\n",
    "    \"3-9-1-paired-t-test.csv\")\n",
    "paired_test_data"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T20:13:07.982773Z",
     "end_time": "2024-04-16T20:13:08.038071Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 4. 实现：配对样本 t 检验"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 0.6, -0.1,  1.5,  1. ,  0.8])"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 服药前后的样本均值\n",
    "before = paired_test_data.query(\n",
    "    'medicine == \"before\"')[\"body_temperature\"]\n",
    "after = paired_test_data.query(\n",
    "    'medicine == \"after\"')[\"body_temperature\"]\n",
    "# 转为数组类型\n",
    "before = np.array(before)\n",
    "after = np.array(after)\n",
    "# 计算差值\n",
    "diff = after - before\n",
    "diff"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T20:13:08.012689Z",
     "end_time": "2024-04-16T20:13:08.092557Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "data": {
      "text/plain": "TtestResult(statistic=2.901693483620596, pvalue=0.044043109730074276, df=4)"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 检验均值是否与 0 存在差异\n",
    "stats.ttest_1samp(diff, 0)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T20:13:08.042162Z",
     "end_time": "2024-04-16T20:13:08.107324Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "TtestResult(statistic=2.901693483620596, pvalue=0.044043109730074276, df=4)"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 配对样本 t 检验\n",
    "stats.ttest_rel(after, before)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T20:13:08.056738Z",
     "end_time": "2024-04-16T20:13:08.153326Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 6. 实现：独立样本 t 检验"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "data": {
      "text/plain": "3.156"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 均值\n",
    "mean_bef = np.mean(before)\n",
    "mean_aft = np.mean(after)\n",
    "\n",
    "# 方差\n",
    "sigma_bef = np.var(before, ddof=1)\n",
    "sigma_aft = np.var(after, ddof=1)\n",
    "\n",
    "# 样本容量\n",
    "m = len(before)\n",
    "n = len(after)\n",
    "\n",
    "# t 值\n",
    "t_value = (mean_aft - mean_bef) / np.sqrt((sigma_bef / m + sigma_aft / n))\n",
    "t_value"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T20:13:08.085382Z",
     "end_time": "2024-04-16T20:13:08.153326Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "data": {
      "text/plain": "TtestResult(statistic=3.1557282344421034, pvalue=0.013484775682079892, df=7.998478291882638)"
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stats.ttest_ind(after, before, equal_var=False)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T20:13:08.100802Z",
     "end_time": "2024-04-16T20:13:08.184910Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T20:13:08.128561Z",
     "end_time": "2024-04-16T20:13:08.184910Z"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
